package net.dlz.example.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.dlz.example.domain.Admin;
import net.dlz.example.domain.AdminExample;
import net.dlz.example.domain.SearchAdminResponse;
import net.dlz.example.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    public AdminMapper adminMapper;

    @Override
    public Admin finAdminByName(String adminName) {
        return adminMapper.selectByName(adminName);
    }

    @Override
    public SearchAdminResponse findAllAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> admins = adminMapper.selectByExample(new AdminExample());
        return new SearchAdminResponse(((Page)admins).getTotal(),admins);
    }

    @Override
    public SearchAdminResponse findAdminLinkNmae(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        AdminExample example = new AdminExample();
        example.createCriteria().andAdminNameLike("%"+ keywords + "%");
//        example.createCriteria().andEmailLike("%"+ keywords + "%");
        List<Admin> users = adminMapper.selectByExample(example);
        return new SearchAdminResponse(((Page)users).getTotal(),users);
    }

    @Override
    public int deleteAdmin(String id) {
        return adminMapper.deleteByPrimaryKey(id);
    }
}
